set more off
est drop _all

********************************************************************************
* Explainatory variables and setup
********************************************************************************

use scratchdata/ncds_combined.dta, clear

ge Creativity=. 

* controls for pref. specification
local ControlsC  $Controls Creativity $TestC 
local ControlsCa $Controls Creativity $TestC i.age 

* control for robustness checks
local ControlsC_notee $Controls_notee Creativity $TestC 			/* without control for teacher evaluation error*/
local ControlsCa_notee $Controls_notee Creativity $TestC i.age

local ControlsI $Controls Creativity $TestI 						/* with controls for 5 separate cog. ability measures*/
local ControlsIa $Controls Creativity $TestI i.age					

local ControlsNCog   $Controls Creativity 							/* without cognitive ability */
local ControlsNCoga  $Controls Creativity i.age					


local ControlsGA  $Controls Creativity generalab_sd_11 				/* Cognitive ability measured using age 11 general ability only*/
local ControlsGAa $Controls Creativity generalab_sd_11 i.age		

* Education controls with zeros for missing and missing indicators
ge MissingQual16 = Qual16==.
ge mQual16 = Qual16==1

ge MissingQual18 = Qual18==.
ge mQual18 = Qual18==1

ge MissingUniQual = UniQual==.
ge mUniQual = UniQual==1

ge MissingRUniQual = RUniQual==.
ge mRUniQual = RUniQual==1

* occupation controls with zeros for missing and missing indicators
foreach var in I55 I50 I46 I42 I33 {
	
    // Generate CSC variables
    gen CSC_`var' = SC_`var'

    // Replace missing values with 99
    replace CSC_`var' = 99 if CSC_`var' == .

    // Multiply by 10 and round
    replace CSC_`var' = round(10 * CSC_`var')

    // Generate frequency table variables
    tabulate CSC_`var', generate(cCSC_`var'_)
	
	drop  CSC_`var'
	rename cCSC_`var'_*  cCSC_*_`var'
	
}

// occupational category missing at 23
ge  cCSC_1_I23=.


program drop _all 

capture drop program spec

program define spec, rclass
    args S ControlsC ControlsC_notee ControlsI ControlsNCog ControlsGA

	if `S'==1{
		* pref.
		local keepvars "CogAbility"
		local ipw 1
		local Ct `ControlsC'
		local name "pref"
		local CreativityMeasure "psd"
	}

	if `S'==2{
		* without control for teacher evalaution error
		local keepvars "CogAbility"
		local ipw 1
		local Ct `ControlsC_notee'
		local name "notee"
		local CreativityMeasure "psd"
	}

	if `S'==3{
		* with controls for 5 separate cognitive ability measures
		local keepvars " "
		local ipw 1
		local Ct `ControlsI'
		local name "icog"
		local CreativityMeasure "psd"
	}


	if `S'==4{
		* Using creativity with empirical proportions
		local keepvars "CogAbility"
		local ipw 1
		local Ct `ControlsC'
		local name "epsd"
		local CreativityMeasure "epsd"
	}

	if `S'==5{
		* conditional on education
		local keepvars "CogAbility" 
		local ipw 1
		local Ct `ControlsC' mQual16 mQual18 mUniQual MissingQual16 MissingQual18 MissingUniQual
		local name "CondEduc"
		local CreativityMeasure "psd"
	}


	if `S'==6{
		* Without CA control
		local keepvars " " 
		local ipw 1
		local Ct `ControlsNCog'
		local name "nocog"
		local CreativityMeasure "psd"
	}
	
	
		if `S'==7{
		* conditional on occupational category
		local keepvars "CogAbility" 
		local ipw 1
		local Ct `ControlsC' cCSC* 
		local name "CondOcc"
		local CreativityMeasure "psd"
	}
	


    return local keepvars "`keepvars'"
	return local ipw `ipw'
	return local Ct `Ct'
	return local name `name'
	return local  CreativityMeasure `CreativityMeasure'

end


********************************************************************************
* Educational attainment 
********************************************************************************

local Educ Qual16 Qual18 UniQual
local regs Educ
local opt replaceP

* without p values
foreach S of numlist 1/4 6{
	foreach s of local regs{
		spec `S' "`ControlsC'" "`ControlsC_notee'" "`ControlsI'"  "`ControlsNCog'" "`ControlsGA'"

		do StataSubCode/OutcomesEstOLS.do "`r(Ct)'" "``s''" "`s'_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
			"O-level (age 16)" "A-level (age 18)" "University qualification"

	}
}



* Robusness with alternative measure: uni degree
local EducR RUniQual
local regs EducR
local opt replaceP

forvalues S = 1/1{
	foreach s of local regs{
		spec `S' "`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"

		do StataSubCode/OutcomesEstOLS.do "`r(Ct)'" "``s''" "`s'_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
			"University degree"

	}
}



******************************************************************************************
*  Attributes of the creative
******************************************************************************************


* Obtained O-level (age-16) qual. in subject given obtained at least one O-level qual.
******************************************************************************************

local ExamPassGAPass16 Sub16_Math_passGAPass Sub16_English_passGAPass Sub16_Science_passGAPass Sub16_Art_passGAPass Sub16_Pract_passGAPass  Sub16_Other_passGAPass

local regs ExamPassGAPass16
local opt replaceP

forvalues S = 1/1{
	foreach s of local regs{
		spec `S' "`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"

		do StataSubCode/OutcomesEstOLS.do "`r(Ct)'" "``s''" "`s'_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
			"Math" "English" "Science" "Artistic" "\shortstack{Mostly \\ practical}"  "Other" 

	}
}



* Obtained A-level (age-18) qual. in subject given obtained at least one A-level qual.
******************************************************************************************

local ExamPassGAPass18 Sub18_Math_passGAPass Sub18_English_passGAPass Sub18_Science_passGAPass Sub18_Art_passGAPass Sub18_Pract_passGAPass  Sub18_Other_passGAPass

local regs ExamPassGAPass18
local opt replaceP

forvalues S = 1/1{
	foreach s of local regs{
		spec `S' "`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"

		do StataSubCode/OutcomesEstOLS.do "`r(Ct)'" "``s''" "`s'_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
			"Math" "English" "Science" "Artistic" "\shortstack{Mostly \\ practical}"  "Other" 

	}
}



* Self-reported abiliteis at 16
******************************************************************************************


local AbAvSl  IAbAvMath_I16 IAbAvEnglish_I16 IAbAvScience_I16 IAbAvArt_I16 IAbAvMusic_I16 IAbAvPractical_I16 IAbAvSport_I16
local AlAvSl  IAlAvMath_I16 IAlAvEnglish_I16 IAlAvScience_I16 IAlAvArt_I16 IAlAvMusic_I16 IAlAvPractical_I16 IAlAvSport_I16
local regs AlAvSl AbAvSl

foreach S of numlist 1/4 6 {

	do StataSubCode/WrapperOutcomeOLSAbR.do `S' "`AbAvSl'" "`AlAvSl'" ///
		"`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"

}




* Self-reported skills at 33
**************************************************************************************

local EvalSkPractical  SkE30_I33 SkE24_I33 SkE54_I33 SkE39_I33 
local EvalSkAnalyitic  SkE51_I33 SkE57_I33 SkE36_I33 
local EvalSkMixed      SkE18_I33 SkE21_I33 SkE27_I33 SkE60_I33 
local EvalSkGuiding    SkE42_I33 SkE45_I33 SkE48_I33   
local Skills Skills

forvalues S = 1/6{

	do StataSubCode/WrapperOutcomeOLSkills.do `S' "`EvalSkPractical'" "`EvalSkAnalyitic'" "`EvalSkMixed'" "`EvalSkGuiding'"  ///
		"`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"
}



* Skills required for job at 23
**************************************************************************************

local ReqJobSk SkFq_I23 SkExp_I23 SkTr_I23
local regs ReqJobSk
local opt replaceP

forvalues S = 1/6{
	foreach s of local regs{
		spec `S' "`ControlsC'" "`ControlsC_notee'" "`ControlsI'" "`ControlsNCog'" "`ControlsGA'"

		do StataSubCode/OutcomesEstOLS.do "`r(Ct)'" "``s''" "`s'_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
			"Formal qualification" "Experience" "Training"
	}
}



********************************************************************************
* Work in a creative occupation
********************************************************************************

spec 1 "`ControlsCa'" "`ControlsCa_notee'" "`ControlsIa'" "`ControlsNCoga'"
do StataSubCode/OutcomesCreativeOccEmpSE.do  "`r(Ct)'" "`r(CreativityMeasure)'" 

********************************************************************************
* Employment, earnings & hours
********************************************************************************

* trim
foreach a of numlist 23 33 42 46 50 55{
	qui centile lwage_I`a', centile(1 99)
	gen touse = inrange(lwage_I`a', r(c_1), r(c_2))
	su touse if lwage_I`a'!=.
	qui replace lwage_I`a'=.  if touse==0
	qui replace lpay_I`a'=.   if touse==0
	qui replace lhours_I`a'=. if touse==0
	drop touse
}


* effect on self employment and work in a creative occ
spec 1 "`ControlsCa'" "`ControlsCa_notee'" "`ControlsIa'" "`ControlsNCoga'" "`ControlsGA'"
do StataSubCode/OutcomesSE.do  "`r(Ct)'" "`r(CreativityMeasure)'" 



* pooled OLS
local opt replaceP

forvalues S = 1/7{

	spec `S' "`ControlsCa'" "`ControlsCa_notee'" "`ControlsIa'" "`ControlsNCoga'" "`ControlsGA'"

	do StataSubCode/OutcomeOLSPooled.do `S' "`r(Ct)'" "pooled_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'" ///
		"Log hourly earnings" "Log weekly earnings" "Log weekly hours"  "In work" 

}


* binary measuresures with interaction
do StataSubCode/OutcomesBinarySkills.do "Log hourly earnings" "Log weekly earnings" "Log weekly hours"  "In work" 


********************************************************************************
* Occupational categories
********************************************************************************

* pooled OLS
local opt replaceP
forvalues S = 1/6{

	spec `S' "`ControlsCa'" "`ControlsCa_notee'" "`ControlsIa'" "`ControlsNCoga'" "`ControlsGA'"

	do StataSubCode/PooledOLSOcc.do "`r(Ct)'" "occ_`r(name)'" "`r(keepvars)'" "`r(ipw)'" "`r(CreativityMeasure)'"

}






